package com.uam.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uam.common.file.FileToStringUtils;
import com.uam.db.mapper.PointMapper;
import com.uam.db.model.entity.Point;
import com.uam.service.PointService;
import com.uam.utils.JsonUtils;
import com.uam.utils.StringUtils;
import com.uam.web.common.model.PageRes;
import com.uam.web.common.model.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author uam
 * @create 2023/12/3 14:15
 * @email uam@uam.com
 */
@Service
public class PointServiceImpl implements PointService {
    @Autowired
    private PointMapper pointMapper;

    @Override
    public Result upload(String data) {
        List<Point> pointlist = JsonUtils.getPointlist(data);
        pointlist.forEach(pointMapper::insert);
        return Result.createSuccess("成功");
    }

    @Override
    public Result list(Integer pageNum, Integer pageSize, String pointName) {
        pageNum = 0;
        pageSize = 10000;
        Page<Point> page = new Page<>(pageNum, pageSize);
        // QueryWrapper<Point> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<Point> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(pointName), Point::getPointName, pointName);
        Page<Point> pointPage = pointMapper.selectPage(page, queryWrapper);
        PageRes<Point> data = new PageRes<>(pointPage.getRecords(), pointPage.getTotal());
        return Result.createSuccess(data);
    }

    @Override
    public List<Point> list(Integer pageNum, Integer pageSize, List<String> pointName) {

        Page<Point> page = new Page<>(pageNum, pageSize);
        // QueryWrapper<Point> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<Point> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(CollectionUtils.isNotEmpty(pointName), Point::getPointName, pointName);
        Page<Point> pointPage = pointMapper.selectPage(page, queryWrapper);
        return pointPage.getRecords();

    }

    @Override
    public Result uploadFile(MultipartFile file) {
        String s = FileToStringUtils.txt2String(file);
        List<Point> pointlist = JsonUtils.getPointlist(s);
        pointlist.forEach(pointMapper::insert);
        return Result.createSuccess("成功");
    }

    public JSONObject getPoint2GeoJson(int status) {
        LambdaQueryWrapper<Point> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Point::getStatus, status);
        List<Point> points = pointMapper.selectList(queryWrapper);
        return JsonUtils.pointToGeoJson(points, status == 1 ? "常规起降点" : "备用起降点");
    }
}
